package com.bs.service.impl;

import java.util.*;
import com.bs.bean.Orders;
import com.bs.service.OrdersService;
import com.bs.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
/*业务层：OrdersService接口的实现类，调用OrdersDao接口的实现类对数据进行相关业务等操作*/
public class OrdersServiceImpl implements OrdersService{

//	注入OrdersDao接口的实现类对象
	@Autowired
	OrdersMapper ordersDao;
//	根据条件查询orders中的数据
	public List<Orders> findAll(Orders query) {
		// TODO Auto-generated method stub
		return findAll(query==null?new Orders():query,true);
	}
	
//	根据条件和模式查询orders中的数据
	public List<Orders> findAll(Orders whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Orders> list = ordersDao.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Orders> iterator = list.iterator();
			while (iterator.hasNext()) {
				Orders orders = (Orders) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (mode) {
						if (!(orders.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getUserid() != null&&(!(whereWrap.getUserid()+"").equals(""))) {
					if (mode) {
						if (!(orders.getUserid() + "").equals(whereWrap.getUserid() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getUserid() + "").contains(whereWrap.getUserid() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getUsername() != null&&(!(whereWrap.getUsername()+"").equals(""))) {
					if (mode) {
						if (!(orders.getUsername() + "").equals(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getUsername() + "").contains(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getSex() != null&&(!(whereWrap.getSex()+"").equals(""))) {
					if (mode) {
						if (!(orders.getSex() + "").equals(whereWrap.getSex() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getSex() + "").contains(whereWrap.getSex() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPhone() != null&&(!(whereWrap.getPhone()+"").equals(""))) {
					if (mode) {
						if (!(orders.getPhone() + "").equals(whereWrap.getPhone() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getPhone() + "").contains(whereWrap.getPhone() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getInstrumentid() != null&&(!(whereWrap.getInstrumentid()+"").equals(""))) {
					if (mode) {
						if (!(orders.getInstrumentid() + "").equals(whereWrap.getInstrumentid() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getInstrumentid() + "").contains(whereWrap.getInstrumentid() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getName() != null&&(!(whereWrap.getName()+"").equals(""))) {
					if (mode) {
						if (!(orders.getName() + "").equals(whereWrap.getName() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getName() + "").contains(whereWrap.getName() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getRate() != null&&(!(whereWrap.getRate()+"").equals(""))) {
					if (mode) {
						if (!(orders.getRate() + "").equals(whereWrap.getRate() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getRate() + "").contains(whereWrap.getRate() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPrice() != null&&(!(whereWrap.getPrice()+"").equals(""))) {
					if (mode) {
						if (!(orders.getPrice() + "").equals(whereWrap.getPrice() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getPrice() + "").contains(whereWrap.getPrice() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPic() != null&&(!(whereWrap.getPic()+"").equals(""))) {
					if (mode) {
						if (!(orders.getPic() + "").equals(whereWrap.getPic() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getPic() + "").contains(whereWrap.getPic() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getState() != null&&(!(whereWrap.getState()+"").equals(""))) {
					if (mode) {
						if (!(orders.getState() + "").equals(whereWrap.getState() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(orders.getState() + "").contains(whereWrap.getState() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}
	
//	查询orders中的数据
	public List<Orders> findAll() {
		// TODO Auto-generated method stub
		return findAll(new Orders());
	}
	
//	根据主键查询orders中的一条数据
	public Orders findById(Integer id) {
		// TODO Auto-generated method stub
		return ordersDao.selectByPrimaryKey(id);
	}

//	选择性添加orders表中的orders数据
	public int insert(Orders orders) {
		// TODO Auto-generated method stub
		return ordersDao.insertSelective(orders);
	}

//	根据主键更新orders表中的一条记录
	public int updateById(Orders orders) {
		// TODO Auto-generated method stub
		return ordersDao.updateByPrimaryKey(orders);
	}

//	根据主键选择性更新orders表中的orders数据
	public int updateByIdSelective(Orders orders) {
		// TODO Auto-generated method stub
		Orders dbOrders = findById(orders.getId());
		if (orders.getId()==null) {
			orders.setId(dbOrders.getId());
		}
		if (orders.getUserid()==null) {
			orders.setUserid(dbOrders.getUserid());
		}
		if (orders.getUsername()==null) {
			orders.setUsername(dbOrders.getUsername());
		}
		if (orders.getSex()==null) {
			orders.setSex(dbOrders.getSex());
		}
		if (orders.getPhone()==null) {
			orders.setPhone(dbOrders.getPhone());
		}
		if (orders.getInstrumentid()==null) {
			orders.setInstrumentid(dbOrders.getInstrumentid());
		}
		if (orders.getName()==null) {
			orders.setName(dbOrders.getName());
		}
		if (orders.getRate()==null) {
			orders.setRate(dbOrders.getRate());
		}
		if (orders.getPrice()==null) {
			orders.setPrice(dbOrders.getPrice());
		}
		if (orders.getPic()==null) {
			orders.setPic(dbOrders.getPic());
		}
		if (orders.getState()==null) {
			orders.setState(dbOrders.getState());
		}
		return updateById(orders);
	}

//	根据条件删除orders表中的数据
	public int delete(Orders whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap,true);
	}

//	根据条件和模式删除orders表中的数据
	public int delete(Orders whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Orders> list = findAll(whereWrap, mode);
		for (Orders orders : list) {
			deleteById(orders.getId());
		}
		return list.size();
	}
//	根据主键删除orders表中的一条记录
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return ordersDao.deleteByPrimaryKey(id);
	}

//	根据主键集合批量删除orders表中的记录
	public int deleteByIds(List<Integer> list){
		for (Integer id : list) {
			deleteById(id);
		}
		return list.size();
	}
}
